//
//  ZTXOwnerWayBillViewController.m
//  NtoccShipper
//
//  Created by 张天兴 on 2023/6/27.
//  Copyright © 2023 zhuchongjin. All rights reserved.
//

#import "ZTXOwnerWayBillViewController.h"

@interface ZTXOwnerWayBillViewController ()
@property (nonatomic, strong) NSString* erase;
@property (nonatomic, strong) NSString *settlementWeight;
@property (nonatomic, strong) NSString* type;
@end

@implementation ZTXOwnerWayBillViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if([self.titleText isEqualToString:@"承运单价"]){
        [self setTitleName:CJLString(@"司机计算规则")];
        self.textFidelNameTxt.text = @"承运单价";
        self.type = @"0";
        self.textFieldNumbder.placeholder = @"请输入承运单价";
    }else{
        [self setTitleName:CJLString(@"货主计算规则")];
        self.textFidelNameTxt.text = @"成交单价";
        self.type = @"1";
        self.textFieldNumbder.placeholder = @"请输入成交单价";
    }
    self.erase = @"0";
    self.settlementWeight = @"0";
    if([self.isTrunkName isEqualToString:@"元/车"]){
        if([self.titleText isEqualToString:@"承运单价"]){
            [self.zhuangHNumberTypeButton setTitle:CJStringWithFormat(@"%.2f元/车",self.realPrice) forState:(UIControlStateNormal)];
        }else{
            [self.zhuangHNumberTypeButton setTitle:CJStringWithFormat(@"%.2f元/车",self.taxFee) forState:(UIControlStateNormal)];
        }
    }else{
        [self.zhuangHNumberTypeButton setTitle:@"按装货数量" forState:(UIControlStateNormal)];
    }
    [self.moLingNumberTypeButton setTitle:@"不抹零" forState:(UIControlStateNormal)];
    [self setRightImage:CJLString(@"确定") image:@""];
    [self setBackLeftButton:@""];
    self.toHeight.constant = kNavBarHeight + 10;
}
- (void)RightTextClick:(UIButton *)sender{
    if([self.textFieldNumbder.text floatValue] <= 0){
        [CJAlert showStringDismissWithDelay:@"请填写正确的成交价格"];
        return;
    }
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/orderDetail/ByPrice",CJBaseUrl);
    NSDictionary *dicPar;
    if([self.isTrunkName isEqualToString:@"元/车"]){
        if([self.titleText isEqualToString:@"承运单价"]){
            dicPar = @{@"price":self.textFieldNumbder.text,
                                     @"settlementWeight":self.settlementWeight,
                                     @"type":self.type, //0:司机计算规则，1货主计算规则
                                     @"erase":self.erase,//0:不抹零.1抹零计算.2:5元下抹零.3:10元下抹零
                                     @"mainOrderCode":self.strCode,
                                     @"unit":@(3)
            };
        }else{
            dicPar = @{@"price":self.textFieldNumbder.text,
                                     @"settlementWeight":self.settlementWeight,
                                     @"type":self.type, //0:司机计算规则，1货主计算规则
                                     @"erase":self.erase,//0:不抹零.1抹零计算.2:5元下抹零.3:10元下抹零
                                     @"mainOrderCode":self.strCode,
                                     @"unit":@(3)
            };
        }
    }else{
        dicPar = @{@"price":self.textFieldNumbder.text,
                                 @"settlementWeight":self.settlementWeight,
                                 @"type":self.type, //0:司机计算规则，1货主计算规则
                                 @"erase":self.erase,//0:不抹零.1抹零计算.2:5元下抹零.3:10元下抹零
                                 @"mainOrderCode":self.strCode,
                                 @"unit":self.coalUnit
        };
    }
 
    
    WS(ws);
    [[HDNetworking sharedHDNetworking] PUT:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
        
        DLog(@"++++++++%@",responseObject);
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setValue:responseObject[@"Price"] forKey:@"Price"];
        [dic setValue:responseObject[@"UnitPrice"] forKey:@"UnitPrice"];
        [dic setValue:self.erase forKey:@"erase"];
        [dic setValue:ws.textFieldNumbder.text forKey:@"textPrice"];
        if([self.isTrunkName isEqualToString:@"元/车"]){
            [dic setValue:ws.settlementWeight forKey:@"settlementWeight"];
        }else{
            [dic setValue:ws.settlementWeight forKey:@"settlementWeight"];
        }
        ws.blockRuturnSucess(dic);
        [ws pop];
    } failure:^(NSError * _Nonnull error) {
        DLog(@"++++++++%@",error);
        
    }];
    
   
}
- (IBAction)jieSuanAction:(UIButton *)sender {
    if([self.isTrunkName isEqualToString:@"元/车"]){
        if([self.titleText isEqualToString:@"承运单价"]){
            [self.zhuangHNumberTypeButton setTitle:CJStringWithFormat(@"%.2f元/车",self.realPrice) forState:(UIControlStateNormal)];
        }else{
            [self.zhuangHNumberTypeButton setTitle:CJStringWithFormat(@"%.2f元/车",self.taxFee) forState:(UIControlStateNormal)];
        }
    }else{
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *change = [UIAlertAction actionWithTitle:@"按装货数量" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            self.settlementWeight = self.loadQuantity;
            self.settlementWeight = @"0";
            [self.zhuangHNumberTypeButton setTitle:@"按装货数量" forState:(UIControlStateNormal)];
        }];
        UIAlertAction *seeBig = [UIAlertAction actionWithTitle:@"按卸货数量" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//            self.settlementWeight = self.unloadQuantity;
            self.settlementWeight = @"1";
            [self.zhuangHNumberTypeButton setTitle:@"按卸货数量" forState:(UIControlStateNormal)];
        }];
        UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"按装卸货数量最小的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.settlementWeight = @"2";
//            if(self.unloadQuantity >= self.loadQuantity){
//                self.settlementWeight = self.unloadQuantity;
//            }else{
//                self.settlementWeight = self.loadQuantity;
//            }
            [self.zhuangHNumberTypeButton setTitle:@"按装卸货数量最小的" forState:(UIControlStateNormal)];
        }];

        [alertVC addAction:change];
        [alertVC addAction:seeBig];
        [alertVC addAction:cancle];
        [self presentViewController:alertVC animated:YES completion:nil];
    }

}

- (IBAction)moLingAction:(UIButton *)sender {
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *change = [UIAlertAction actionWithTitle:@"不抹零" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.erase = @"0";
        [self.moLingNumberTypeButton setTitle:@"不抹零" forState:(UIControlStateNormal)];
    }];
    UIAlertAction *seeBig = [UIAlertAction actionWithTitle:@"角分抹零" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.erase = @"1";
        [self.moLingNumberTypeButton setTitle:@"角分抹零" forState:(UIControlStateNormal)];

    }];
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"五元下抹零" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.erase = @"2";
        [self.moLingNumberTypeButton setTitle:@"五元下抹零" forState:(UIControlStateNormal)];

    }];
    UIAlertAction *cancleTen = [UIAlertAction actionWithTitle:@"十元下抹零" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.erase = @"3";
        [self.moLingNumberTypeButton setTitle:@"十元下抹零" forState:(UIControlStateNormal)];
    }];
    [alertVC addAction:change];
    [alertVC addAction:seeBig];
    [alertVC addAction:cancle];
    [alertVC addAction:cancleTen];
    [self presentViewController:alertVC animated:YES completion:nil];
}

@end
